<?php

namespace app\admin\controller\vip;

use app\common\controller\Backend;
use app\common\model\VipModel;
use app\common\model\VipOrderModel;

/**
 * 会员vip订单管理
 * Class VipOrder
 * @package app\admin\controller\vip\VipOrder
 */
class VipOrder extends Backend
{

    /**
     * 模型对象
     * @var \app\common\model\VipOrderModel
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new VipOrderModel();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 首页
     * @return mixed|string|\think\response\Json
     */
    public function index()
    {
        if (request()->isAjax()) {
            $page      = input('param.page/d', 1);
            $rows      = input('param.rows/d', 20);
            $key       = input('param.keyword', '');
            $type      = input('param.type', '');
            $status    = input('param.status', '');
            $vid       = input('param.vid', '');
            $startTime = input('param.start_time', '');
            $endTime   = input('param.end_time', '');

            $result = $this->model->getLists([
                'keyword'    => $key,
                'type'       => $type,
                'status'     => $status,
                'vid'        => $vid,
                'start_time' => $startTime,
                'end_time'   => $endTime,
            ], $rows, $page, 'a.*,b.nickname,b.avatar,b.mobile');

            $result['vipList'] = VipModel::field(['id', 'name'])->select();

            return json($result);
        }

        return $this->fetch();
    }

    /**
     * 添加vip订单
     * @return \think\response\Json
     */
    public function vip()
    {
        $data = input('post.');
        $id   = $this->model->add($data);

        if (!empty($id)) {
            return json([
                'code' => 200,
                'msg' => '提交成功'
            ]);
        } else {
            return json([
                'code' => 400,
                'msg' => '提交失败'
            ]);
        }
    }

    // 删除
    public function dell()
    {
        $id = input('id');
        if (!empty($id)) {
            $ids = strpos($id, ',') !== false ? explode(',', $id) : [$id];
            foreach ($ids as $v) {
                if (!$this->model->getItem((int)$v)) {
                    return json(['code' => 401, 'msg' => '您要删除的数据不存在']);
                }
            }
            foreach ($ids as $v) {
                $this->model->del($v);
            }
            return json(['code' => 200, 'msg' => '删除成功']);
        }
    }

}
